package org.windowkit.toolkit.widget.table.render;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;

import org.ddevil.data.Data;
import org.windowkit.toolkit.widget.table.RecordTableModel;

/**
 * A base class for a decorator that can be used to render a different background color
 * based on the data that is being represented.
 * @author Rich O'Connell
 */
public abstract class AbstractBackgroundRenderer extends
		AbstractTableCellRendererDecorator {

	private boolean forceBlackFont = true;

	/**
	 * Constructor.
	 */
	public AbstractBackgroundRenderer() {
		super();
	}

	/**
	 * @param decorateMe
	 */
	public AbstractBackgroundRenderer(final TableCellRenderer decorateMe) {
		super(decorateMe);
	}

	/**
	 * @return forceBlackFont
	 */
	protected boolean isForceBlackFont() {
		return forceBlackFont;
	}

	/**
	 * A flag that allows you to force the renderer to always use a black font.
	 * Defaults to true.
	 * @param forceBlackFont forceBlackFont
	 */
	public void setForceBlackFont(final boolean forceBlackFont) {
		this.forceBlackFont = forceBlackFont;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void decorate(final Component c, final Data d, final RecordTableModel model,
			final JTable table, final Object value, final CellInfo info) {

		Color bg = getColor(c, d, model, table, value, info);
		if (bg != null) {
			c.setBackground(bg);
		}
		if (isForceBlackFont()) {
			c.setForeground(Color.BLACK);
		}
	}

	/**
	 * Extension point where you choose what color the background should be based on the
	 * data for the row being rendered.
	 * @param c component that represents the cell being rendered
	 * @param d the data for the row
	 * @param model the table model
	 * @param table the table component
	 * @param value the value being rendered
	 * @param info object containing info about the cell being rendered
	 * @return the Color that the cell's background should be rendered
	 */
	protected abstract Color getColor(Component c, Data d, RecordTableModel model,
			JTable table, Object value, CellInfo info);

}
